/*****************************************************************************/
/**
 *  @file   PixelSend.cpp
 *  @author Allan Lorant, Naohisa Sakamoto
 */
/*----------------------------------------------------------------------------
 *
 *  Copyright (c) Visualization Laboratory, Kyoto University.
 *  All rights reserved.
 *  See http://www.viz.media.kyoto-u.ac.jp/kvs/copyright/ for details.
 *
 *  $Id$
 */
/*****************************************************************************/
#include <string>
#include <kvs/Version>
#include <kvs/File>
#include <kvs/TransferFunction>
#include <kvs/KVSMLObjectPoint>
#include <kvs/KVSMLObjectStructuredVolume>
#include <kvs/KVSMLObjectUnstructuredVolume>
#include <kvs/PointImporter>
#include <kvs/StructuredVolumeImporter>
#include <kvs/UnstructuredVolumeImporter>
#include <kvs/HydrogenVolumeData>
#include <kvs/PointObject>
#include <kvs/StructuredVolumeObject>
#include <kvs/UnstructuredVolumeObject>
#include <kvs/CellByCellMetropolisSampling>
#include "ParticleSend.h"


int main( int argc, char** argv )
{
    const char* data = argv[1];
    const int repeat = std::atoi( argv[2] );

    const int subpixel = static_cast<int>( std::sqrt( repeat ) + 0.5 );
    const float step = 0.5f;
    const kvs::TransferFunction tfunc( 256 );

    kvs::PointObject* object = NULL;
    if ( std::string( data ) == "test" )
    {
        kvs::StructuredVolumeObject* volume = new kvs::HydrogenVolumeData( kvs::Vector3ui( 32, 32, 32 ) );
        object = new kvs::CellByCellMetropolisSampling( volume, subpixel, step, tfunc );
        delete volume;
    }
    else
    {
#if ( KVS_VERSION_MAJOR == 1 )
        if ( kvs::File( data ).isExisted() )
#elif ( KVS_VERSION_MAJOR == 2 )
        if ( kvs::File( data ).exists() )
#endif
        {
            if ( kvs::KVSMLObjectPoint::CheckFileFormat( data ) )
            {
                object = new kvs::PointImporter( std::string( data ) );
            }
            else if ( kvs::KVSMLObjectStructuredVolume::CheckFileFormat( data ) )
            {
                kvs::StructuredVolumeObject* volume = new kvs::StructuredVolumeImporter( std::string( data ) );
                object = new kvs::CellByCellMetropolisSampling( volume, subpixel, step, tfunc );
                delete volume;
            }
            else if ( kvs::KVSMLObjectUnstructuredVolume::CheckFileFormat( data ) )
            {
                kvs::UnstructuredVolumeObject* volume = new kvs::UnstructuredVolumeImporter( std::string( data ) );
                object = new kvs::CellByCellMetropolisSampling( volume, subpixel, step, tfunc );
                delete volume;
            }
            else
            {
                std::cout << "Cannot import '" << std::string( data ) << "'." << std::endl;
            }
        }
        else
        {
            std::cout << "Cannot find '" << std::string( data ) << "'." << std::endl;
        }
    }

    if ( !object )
    {
        std::cout << "Cannot generate particles." << std::endl;
        return 1;
    }

    ParticleSend::Interface interface;
    if ( !interface.send( object ) )
    {
        std::cout << "Cannot send particles." << std::endl;
        return 1;
    }

    delete object;

    return 0;
}
